using System;
using AltNetHispano.Vale.DataAnnotations.Tests.SampleEntities;
using NUnit.Framework;
using SharpTestsEx;

namespace AltNetHispano.Vale.DataAnnotations.Tests
{
	public class ValidatorTest
	{
		private IValidator validator;

		[SetUp]
		public void SetUp()
		{
			validator = new Validator();
			validator.RegisterDataAnnotationsConstraints<Person>();
			validator.RegisterDataAnnotationsConstraints<Address>();			
		}

		[Test]
		public void WhenValidatePersonWithDefaultValuesThenResturnsInvalidMessages()
		{
			var person = new Person { Name = null, Surname = null, Birthday = DateTime.Today };
			var invalidMessages = validator.GetInvalidMessages(person);
			invalidMessages.Should().Contain("Name is required.");
			invalidMessages.Should().Contain("Surname is required.");
			invalidMessages.Should().Contain("Birthday should be in the past.");
		}

		[Test]
		public void WhenValidateAddressInvalidWithDefaultValuesThenReturnInvalidMessagges()
		{
			var address = new Address { City = null };
			var invalidMessages = validator.GetInvalidMessages(address);
			invalidMessages.Should().Contain("City is required.");
		}

		[Test]
		public void WhenRegisterAValidationThenAddValidationToValidators()
		{
			var validator1 = new Validator();
			validator1.RegisterDataAnnotationsConstraints<Person>();
			validator1.ValidatorsHolder.Keys.Should().Contain(typeof(Person));
			validator1.ValidatorsHolder[typeof(Person)].Should().Have.Count.EqualTo(3);
		}
		
		[Test]
		public void WhenValidateValidPersonThenIsValidResturnTrue()
		{
		    var person = new Person { Name = "0123456789", Surname = "0123456789", Birthday = DateTime.Today.AddDays(-10) };
		    validator.IsValid(person).Should().Be.True();
		}
	}
}